package serivce;

import dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import untity.User;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Service
public class LoginSerivceImp implements LoginService{


    @Autowired
    private UserDao userDao;
    public HashMap<String,Object> login(String account,String password){

        HashMap flag=userDao.login(account,password);

        System.out.println(flag);
        return flag;
    }

    @Override
    public String setLoginSession(HttpServletRequest request,String id,String userName) {
        HttpSession hs= request.getSession();
        hs.setAttribute("id",id);
        System.out.println("session=="+hs.getId());
        hs.setAttribute("userName",userName);
        return null;
    }

    @Override
    public HashMap<String, Object> checkSession(HttpServletRequest request) {
        HttpSession hs=request.getSession();
        System.out.println("session=="+hs.getId());
        String id= (String) hs.getAttribute("id");
        if (id==null){
            return null;
        }
        String userName=(String)hs.getAttribute("userName");
        HashMap<String,Object> hashMap=new HashMap<String,Object>();
        hashMap.put("id",Integer.valueOf(id));
        hashMap.put("userName",userName);
        return hashMap;
    }

    @Override
    public String addUser(User user) {
        userDao.addUser(user);
        return null;
    }

    @Override
    public String queryByAccount(String account) {
        String flag=userDao.queryByAccount(account);
        return flag;
    }


}
